package at.dl.android.dota.adviser;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

public class ListItemActivity extends ListActivityWithDB {

    public final static String SEARCH_ITEM_WHERE_CAUSE = "SEARCH_ITEM_WHERE_CAUSE";
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate ( Bundle savedInstanceState ) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_items);
        
        final Bundle extras = getIntent().getExtras();
        final String where = extras.getString(SEARCH_ITEM_WHERE_CAUSE);
        final String query = "SELECT " + ColumnNames.Items.name + " AS " + ColumnNames.Alias._id + ", " + ColumnNames.Items.total_costs + ", " + ColumnNames.Items.item_image_id + " FROM ITEMS" + where;
        SQLiteDatabase db = getDataBaseHelper().getReadableDatabase();
        final Cursor cursor = db.rawQuery(query,new String[0]);
        startManagingCursor(cursor); // do we need that?
        setListAdapter(new MyItemEntryAdapter(this,cursor));
    }

    @Override
    protected void onListItemClick ( ListView l, View v, int position, long id ) {
        super.onListItemClick(l,v,position,id);
        
        TextView t = (TextView) v.findViewById(R.id.name);
        String name1 = t.getText().toString();
        onPause();
        Intent myIntent = new Intent(v.getContext(), ShowItemActivity.class);
        myIntent.putExtra(ShowItemActivity.ITEM_NAME,name1);
        startActivity(myIntent);
    }
    
}
